package android.pot.com.smartcar.ui.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.pot.com.smartcar.R;
import android.pot.com.smartcar.application.MyApplication;
import android.pot.com.smartcar.base.CommonAdapter;
import android.pot.com.smartcar.base.ViewHolder;
import android.pot.com.smartcar.base.swipe.SwipeBackActivity;
import android.pot.com.smartcar.entity.AreaEntity;
import android.pot.com.smartcar.entity.PlatFormEntity;
import android.pot.com.smartcar.widget.ListViewForScrollView;
import android.pot.com.smartcar.widget.MyGridView;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by zhaoshilu on 2017/11/27.
 */
public class AreaSelectActivity extends SwipeBackActivity {

    private  MyAdapter mListAdapter;

    private PlatFormEntity entity;

    private TextView tv_default;

    private int cid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_area_select);
        initData();
        initView();
        addListener();
    }

    @Override
    protected void initView() {
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("区域选择");
        tv_default = (TextView) findViewById(R.id.tv_default);
        initListView();
    }


    @Override
    protected void addListener() {
        findViewById(R.id.img_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollToFinishActivity();
            }
        });
        findViewById(R.id.tv_default).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_default.setTextColor(ContextCompat.getColor(MyApplication.getContext(),R.color.white));
                tv_default.setBackgroundResource(R.drawable.corner_black_bg);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Intent intent = new Intent();
                        intent.putExtra("areaId",-1);
                        intent.putExtra("areaName","我的位置");
                        setResult(3000,intent);
                        scrollToFinishActivity();
                    }
                }, 300);



            }
        });
    }

    private void initListView(){
        Bundle bundle = getIntent().getExtras();
        AreaEntity areaEntity = (AreaEntity) bundle.getSerializable("data");
        ListView lv_list = (ListViewForScrollView) findViewById(R.id.lv_list);
        mListAdapter = new MyAdapter(areaEntity.getData());
        lv_list.setAdapter(mListAdapter);

    }


    @Override
    protected void initData() {
        Bundle bundle = getIntent().getExtras();
        entity = (PlatFormEntity) bundle.getSerializable("list");

    }

    class MyAdapter extends BaseAdapter {
        List<AreaEntity.AreaOutData> data;    //数据源

        public MyAdapter(List<AreaEntity.AreaOutData> data){
            this.data=data;
        }

        @Override
        public int getCount() {
            return data == null ? 0 : data.size();
        }

        @Override
        public AreaEntity.AreaOutData getItem(int position) {
            return data == null ? null : data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView=LayoutInflater.from(AreaSelectActivity.this).inflate(R.layout.listcell_area_select,null);
            final TextView textView = convertView.findViewById(R.id.tv_city);
            textView.setText(getItem(position).getA_name());
            MyGridView gridView = convertView.findViewById(R.id.gv_list);
           final List<AreaEntity.AreaData> listData = data.get(position).getChilds();
             final CommonAdapter mAdapter = new CommonAdapter<AreaEntity.AreaData>(AreaSelectActivity.this,listData,R.layout.gridcell_area_select) {
                @Override
                public void convert(ViewHolder helper, final AreaEntity.AreaData item) {
                    if (item.isSelect()){
                        helper.setTextColor(R.id.tv_type,R.color.white);
                        helper.setBackgroundResource(R.id.tv_type,R.drawable.corner_black_bg);
                    }else{
                        helper.setTextColor(R.id.tv_type,R.color.btn_color_z);

                        helper.setBackgroundResource(R.id.tv_type,R.drawable.corner_storke_1_zz_bg);
                    }
                    helper.setText(R.id.tv_type,item.getA_name());
                    helper.setOnClickListener(R.id.tv_type, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            for (AreaEntity.AreaOutData entity :data){
                                for (AreaEntity.AreaData data: entity.getChilds()){
                                    data.setSelect(false);
                                }
                            }
                            item.setSelect(true);
                            mListAdapter.notifyDataSetChanged();
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    Intent intent = new Intent();
                                    intent.putExtra("areaId",item.getId());
                                    intent.putExtra("areaName",item.getA_name());
                                    setResult(3000,intent);
                                    scrollToFinishActivity();
                                }
                            }, 300);

                        }
                    });
                }
            };
            gridView.setAdapter(mAdapter);
            return convertView;
        }
    }



}